function createInject() {
    // 将inject.js以script标签的方式嵌入网页dom
    var temp = document.createElement('script');
    temp.setAttribute('type', 'text/javascript');
    temp.setAttribute('charset', 'UTF-8');
    temp.src = chrome.extension.getURL('/static/js/inject.js');
    document.body.appendChild(temp);
}

// 创建按钮
// 需求：点击复制当前页面的职位数据
var student,parentName,childName;
$(document).ready(function () {
    if(canRender()){
        $('body').append(render());
        createInject();
    }
});
function render() {
    let elements = '<div id="togoApp">\n' +
        '    <div class="get-data">\n' +
        '        抓取<br>数据\n' +
        '    </div>\n' +
        '    <div class="show-data">\n' +
        '        查看<br>数据\n' +
        '        <div class="table-box">\n' +
        '            <h4>求职推荐表</h4>\n' +
        '            <div class="info">\n' +
        '                <div><span>学生姓名：</span>\n' +
        '                <span id="togo-name"></span></div>\n' +
        '                <div><span>求职方向：</span>\n' +
        '                <span id="togo-job"></span></div>\n' +
        '            </div>\n' +
        '            <div class="table-view">\n' +
        '                <table id="togo-table">\n' +
        '                    <thead>\n' +
        '                    <tr>\n' +
        '                        <td>操作</td>\n' +
        '                        <td>序号</td>\n' +
        '                        <td>公司</td>\n' +
        '                        <td>岗位名称</td>\n' +
        '                        <td>工作地点</td>\n' +
        '                        <td>薪资水平</td>\n' +
        '                        <td>投递截止时间</td>\n' +
        '                        <td>网页链接</td>\n' +
        '                        <td>备注</td>\n' +
        '                    </tr>\n' +
        '                    </thead>\n' +
        '                    <tbody>\n' +
        '                    </tbody>\n' +
        '                </table>\n' +
        '            </div>\n' +
        '            <div class="togo-btn">\n' +
        '                文件名：<input id="fileName" type="text" placeholder="自定义导出的文件名">\n' +
        '                <a id="getExcel"><span>导出EXCEL</span></a>\n' +
        '            </div>\n' +
        '        </div>\n' +
        '    </div>\n' +
        '</div>'+
        '<textarea id="copyThat" cols="30" rows="10" style="position: absolute;top: 0;opacity: 0;"></textarea>';

    return elements;
}

function canRender(){
    let url = window.location.host;
    switch (url){
        case 'www.lagou.com':
            return true;
            break;
        case 'www.zhipin.com':
            return true;
            break;
        case 'jobs.51job.com':
            return true;
            break;
        case 'jobs.zhaopin.com':
            return true;
            break;
        case 'www.liepin.com':
            return true;
            break;
        case 'campus.liepin.com':
            return true;
            break;
        case 'shixi.xiaozao.org':
            return true;
            break;
        case 'www.shixiseng.com':
            return true;
            break;
        case 'sxxx.haitou.cc':
            return true;
            break;
        case 'job.liuxuehr.com':
            return true;
            break;
        case 'job.dajie.com':
            return true;
            break;
        case 'xiaoyuan.zhaopin.com':
            return true;
            break;
        default:
            return false;
            break;
    }
}

// 接受popup发送过来的消息，并callback
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
    // console.log(sender.tab ?"from a content script:" + sender.tab.url :"from the extension");
    if(request.cmd === 'getUrl'){
        sendResponse(window.location.href);
    }else if(request.cmd === 'open'){
        window.open(request.value);
    }else if(request.cmd === 'refresh'){
        getStorage();
    }
    sendResponse('success');
});

function getStorage() {
    chrome.storage.local.get(function(items) {
        console.log(items);
        student = items.student || {};
        parentName = items.togoBookmark.parentTitle;
        childName = items.togoBookmark.title;
        window.postMessage({type: 'allData',info: items}, '*');
    });
}

window.addEventListener("message", function(e) {
    switch(e.data.type){
        case 'update':{
            if(student[parentName]){
                if(student[parentName][childName]){
                    student[parentName][childName].push(e.data.info);
                }else{
                    student[parentName][childName] = [
                        e.data.info
                    ]
                }
            }else{
                student[parentName] = {
                    [childName]: [
                        e.data.info
                    ]
                }
            }
            break;
        }
        case 'delete':{
            if(e.data.info === 'children'){
                student[parentName][childName] = [];
            }else{
                student[parentName][childName].splice(e.data.info, 1);
            }
            break;
        }
        case 'select':{
            getStorage();
            return;
        }
        case 'edit':{
            student[parentName][childName][e.data.info.index][e.data.info.key] = e.data.info.value;
            break;
        }
        default:{
            return;
        }
    }
    chrome.storage.local.set({student: student}, function(items) {

    })
}, false);